// app.js
import {
  sessionKeyValidityCheck
} from "./utils/network"
App({
  onLaunch() {

    // 判断session_key是否有效，并页面跳转
    sessionKeyValidityCheck()

    // reactiveData中的每个数据都是响应式的，通过调用dxWatch函数可以添加响应式处理函数
    const _this = this
    const handler = {
      get: function (targetObj, key) {
        return Reflect.get(targetObj, key)
      },
      set: function (targetObj, key, newValue) {
        // console.log(this); return 
        if (!_this.globalData.reactiveMethod[key]) { _this.globalData.reactiveMethod[key] = [] }
        _this.globalData.reactiveMethod[key].forEach(fn => {
          fn(newValue)
        })
        Reflect.set(targetObj, key, newValue)
        return true
      }
    };
    this.globalData.reactiveData = new Proxy(this.globalData.rawData, handler)
  },
  globalData: {
    rawData: {},
    reactiveData: {
    },
    reactiveMethod: {},
    userInfo: {
      openid: ''
    },
    currentBook: {}
  },
  dxWatch(attribute, callback) {
    this.globalData.reactiveMethod[attribute].push(callback)
  }
})